package com.seari.hotfixformetroapp.main;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.client.params.ClientPNames;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.seari.hotfixformetroapp.bean.ExcelConfig;
import com.seari.hotfixformetroapp.utils.ImportExcelUtil;

//@Component
//@Order(value = 2)
public class HotfixIec104 implements ApplicationRunner
{
	private StringBuffer buffer = new StringBuffer();
	private final ExcelConfig dataConfig = new ExcelConfig();
	String clientName = "ROUNDPOLLCLIENT";
	@Override
	public void run(ApplicationArguments args) throws Exception
	{
		dataConfig.setInputFileName("104.xlsx");
		dataConfig.setSheetNum(0);
		dataConfig.setDataStartLineNum(1);
		dataConfig.setDataEndLineNum(70);
		//加载excel
        InputStream inputStream = new FileInputStream(new File(dataConfig.getInputFileName()));
        Workbook workbook = ImportExcelUtil.getWorkbook(inputStream, dataConfig.getInputFileName()); 
        processExcelData(workbook);
	}
	
	private void processExcelData(Workbook workbook)
	{
		Sheet sheet = workbook.getSheetAt(dataConfig.getSheetNum());
		Map<String, Iec104Config> resultMap = new HashMap<>();
		for(int i = dataConfig.getDataStartLineNum(); i <= dataConfig.getDataEndLineNum();i++)
		{
			String stationName = "";
			Row row = sheet.getRow(i);
			stationName = row.getCell(3).getStringCellValue();
			Iec104Config iec104Config = null;
			if(!resultMap.containsKey(stationName))
			{
				iec104Config = new Iec104Config();
				iec104Config.setClient(clientName);
				iec104Config.setIp("0.0.0.0");
				iec104Config.setPort(2404);
				iec104Config.setHasCounter(0);
				iec104Config.setName("pscada_"+stationName);
				iec104Config.getIpPortCouple().add(row.getCell(2).getStringCellValue().trim());
			}else {
				iec104Config = resultMap.get(stationName);
				iec104Config.getIpPortCouple().add(row.getCell(2).getStringCellValue().trim());
			}
			resultMap.put(stationName, iec104Config);
		}
		buffer.append("device_list=");
		for (Entry<String, Iec104Config> entry : resultMap.entrySet())
		{
			buffer.append("iec104:pscada_" + entry.getKey() +"|"); //生成设备列表
		}
		buffer.append("\n");
		buffer.append("\n");
		for (Entry<String, Iec104Config> entry : resultMap.entrySet())
		{
			Iec104Config iec104Config = entry.getValue();
			//生成设备配置
			buffer.append(iec104Config.getName()+"_ip=").append(iec104Config.getIp()).append("\n");
			buffer.append(iec104Config.getName()+"_port=").append(iec104Config.getPort()).append("\n");
			buffer.append(iec104Config.getName()+"_stationID=1").append("\n");
			buffer.append(iec104Config.getName()+"_hasCounter=0").append("\n");
			buffer.append(iec104Config.getName()+"_name=").append(iec104Config.getName()).append("\n");
			buffer.append(iec104Config.getName()+"_client=").append(iec104Config.getClient()).append("\n");
			for(int i = 0 ; i < iec104Config.getIpPortCouple().size(); i++)
			{
				if(i==0)
				{
					buffer.append(iec104Config.getName()+"_ip_port_list=").append(iec104Config.getIpPortCouple().get(i)).append("|");
				}else {
					buffer.append(iec104Config.getIpPortCouple().get(i)).append("\n");
				}
			}
			buffer.append("\n");
			buffer.append("\n");
		}
		writeToFile();
	}
	
	private void writeToFile()
	{
		FileWriter fw = null;
		try
		{
			// 如果文件存在，则追加内容；如果文件不存在，则创建文件
			File f = new File("tttt.txt");
			fw = new FileWriter(f, false);
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		PrintWriter pw = new PrintWriter(fw);
		pw.println(buffer.toString());
		pw.flush();
		try
		{
			fw.flush();
			pw.close();
			fw.close();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	//内部类，104配置类
	private class Iec104Config
	{
		private String ip;
		private int port;
		private int stationId;
		private int hasCounter;
		private String name;
		private String client;
		private List<String> ipPortCouple = new ArrayList<String>();
		public String getIp()
		{
			return ip;
		}
		public void setIp(String ip)
		{
			this.ip = ip;
		}
		public int getPort()
		{
			return port;
		}
		public void setPort(int port)
		{
			this.port = port;
		}
		public int getStationId()
		{
			return stationId;
		}
		public void setStationId(int stationId)
		{
			this.stationId = stationId;
		}
		public int getHasCounter()
		{
			return hasCounter;
		}
		public void setHasCounter(int hasCounter)
		{
			this.hasCounter = hasCounter;
		}
		public String getName()
		{
			return name;
		}
		public void setName(String name)
		{
			this.name = name;
		}
		public String getClient()
		{
			return client;
		}
		public void setClient(String client)
		{
			this.client = client;
		}
		public List<String> getIpPortCouple()
		{
			return ipPortCouple;
		}
		public void setIpPortCouple(List<String> ipPortCouple)
		{
			this.ipPortCouple = ipPortCouple;
		}
		
	}

}


